package com.game.jumper.activity;

import java.util.Random;
import java.util.Vector;

import net.youmi.android.banner.AdView;
import net.youmi.android.spot.SpotManager;

import org.loon.framework.android.game.core.graphics.LColor;
import org.loon.framework.android.game.core.graphics.LFont;
import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.Screen;
import org.loon.framework.android.game.core.graphics.component.LButton;
import org.loon.framework.android.game.core.graphics.component.LMessage;
import org.loon.framework.android.game.core.graphics.device.LGraphics;
import org.loon.framework.android.game.core.timer.LTimerContext;

import android.graphics.Bitmap;
import android.media.SoundPool;
import android.view.View;

import com.game.jumper.bean.Hand;
import com.game.jumper.bean.HandState;
import com.game.jumper.bean.Player;
import com.game.jumper.game.Game;
import com.game.jumper.lghelp.ILgHelp;
import com.game.jumper.lghelp.Lg_Back;
import com.game.jumper.lghelp.Lg_Ball;
import com.game.jumper.lghelp.Lg_Board;
import com.game.jumper.lghelp.Lg_Player;

public class PlayScreen extends Screen
{

	// ----��������
	public static int screenW, screenH;// ��Ļ��С

	// ----��Ϸ��ʼ��
	private Game mygame;

	private Vector<ILgHelp> Lgs;

	private Hand hand;

	private Bitmap backimage;

	// ����SoundPool
	private SoundPool sp;

	int sound1;

	static Random ran = new Random();

	void init()
	{
		// ��ʼ��Game
		// canvas=null;
		screenW = this.getWidth();
		screenH = this.getHeight();
		mygame = Game.startGame(screenW, screenH);
		hand = new Hand();
		Lgs = new Vector<ILgHelp>();

		Lgs.addElement(new Lg_Back());
		Lgs.addElement(new Lg_Player(mygame));
		Lgs.addElement(new Lg_Board(mygame));
		Lgs.addElement(new Lg_Ball(mygame));

	}

	@Override
	public void onLoad()
	{
		super.onLoad();
		init();
		initOverWin();
		
	}

	@Override
	public void alter(LTimerContext arg0)
	{
		// TODO Auto-generated method stub
		// mygame.update(hand);
		// mygame.update(hand);
	}

	@Override
	public void draw(LGraphics arg0)
	{
		// TODO Auto-generated method stub
		arg0.drawClear(LColor.gray);
		mygame.update(hand);
		if (mygame.isOver())
		{
			// System.exit(0);
			showOverWin();
			showgoalds();
		}

		// ���û滭����
		if (!arg0.isAntiAlias())
		{
			arg0.setAntiAlias(true);
		}
		for (ILgHelp lg : Lgs)
		{
			lg.draw(arg0);
		}
		// arg0.setFont(14);
		// arg0.drawString(x+","+y, 10, 10);
	}

	@Override
	public void onTouchDown(LTouch arg0)
	{
		// TODO Auto-generated method stub
		hand.setPoint(arg0.getX(), arg0.getY());
		hand.setHandState(HandState.DOWN);
		// mygame.update(hand);
	}

	int x, y;

	@Override
	public void onTouchMove(LTouch arg0)
	{
		// TODO Auto-generated method stub
		hand.setPoint(arg0.getX(), arg0.getY());
		hand.setHandState(HandState.MOVE);
		// mygame.update(hand);

		// x=(int) arg0.getX();
		// y=(int) arg0.getY();
	}

	@Override
	public void onTouchUp(LTouch arg0)
	{
		// TODO Auto-generated method stub
		hand.setPoint(arg0.getX(), arg0.getY());
		hand.setHandState(HandState.UP);
		// mygame.update(hand);
	}

	//
	void resetGame()
	{
		init();
	}

	private void showOverWin()
	{
		resetBtn.setVisible(true);
		cancelBtn.setVisible(true);
	}

	private LButton resetBtn;

	private LButton cancelBtn;

	/**
	 * ��������ʼ����Ϸ�����
	 */
	private void initOverWin()
	{

		/* ���ð�ť */
		resetBtn = new LButton("assets/btn.png")
		{
			@Override
			public void doClick()
			{
				/* ��ʼ��Ϸ */
				resetBtn.setVisible(false);
				cancelBtn.setVisible(false);
				hidegoalds();
				resetGame();

			}

		};
		resetBtn.setImages(new LImage[] { new LImage("assets/btn.png"), new LImage("assets/btnOn.png"), });
		resetBtn.setText("继续");
		int locat = (this.getWidth() - resetBtn.getWidth() * 3) / 2;
		resetBtn.setLocation(locat, this.getHeight() * 0.7);

		/* ȡ��ť */
		cancelBtn = new LButton("assets/btn.png")
		{
			@Override
			public void doClick()
			{
				resetBtn.setVisible(false);
				cancelBtn.setVisible(false);
				hidegoalds();
				System.exit(0);
			}
		};
		cancelBtn.setImages(new LImage[] { new LImage("assets/btn.png"), new LImage("assets/btnOn.png"), });
		cancelBtn.setText("退出");
		cancelBtn.setLocation(this.getWidth() - locat - cancelBtn.getWidth(), this.getHeight() * 0.7);
		setmsg();
		add(resetBtn);
		add(cancelBtn);

		resetBtn.setVisible(false);
		cancelBtn.setVisible(false);

	}

	LMessage msg1;

	LMessage msg2;

	LMessage gameOver;

	void setmsg()
	{
		int leftX = (this.getWidth() - 165) / 2;
		gameOver = new LMessage(this.getWidth(), this.getHalfHeight());
		gameOver.setLocation(leftX, this.getHeight() * 0.2);
		gameOver.setFontColor(LColor.black);
		gameOver.setMessageFont(LFont.getFont(30));
		gameOver.setMessage("Game Over");
		gameOver.setMessageLength(9);
		add(gameOver);
		gameOver.setVisible(false);

		msg1 = new LMessage(this.getWidth(), 50);
		msg1.setLocation(leftX, this.getHeight() * 0.40);
		msg1.setMessageFont(LFont.getFont(20));
		msg1.setFontColor(LColor.black);
		add(msg1);
		msg1.setMessageLength(15);
		msg1.setVisible(false);

		msg2 = new LMessage(this.getWidth(), 50);
		msg2.setLocation(leftX, this.getHeight() * 0.50);
		msg2.setMessageFont(LFont.getFont(20));
		msg2.setFontColor(LColor.black);
		add(msg2);
		msg2.setMessageLength(15);
		msg2.setVisible(false);
	}

	boolean show = false;

	void showgoalds()
	{
		if (!show)
		{
			gameOver.setVisible(true);
			msg1.setMessage("当前分: " + Game.getPlayer().getGoals());
			msg2.setMessage("最高分: " + mygame.getMaxGoals());
			msg1.setVisible(true);
			msg2.setVisible(true);
			show = true;
		}
	}

	void hidegoalds()
	{
		gameOver.setVisible(false);
		msg1.setVisible(false);
		msg2.setVisible(false);
		show = false;
	}
}
